import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		token: '', // 令牌
		userId: '',
		mobile: '',
		showMoney: true, // 是否显示余额
		userInfo: uni.getStorageSync('userInfo') || {}
	},
	mutations: {
		commitToken(state, token) {
			state.token = token;
		},
		commitUserId(state, id) {
			state.userId = id;
		},
		commitMobile(state, num) {
			state.mobile = num;
		},
		commitToggleShowMoney(state, flag) {
			state.showMoney = flag;
		},
		commitUserInfo(state, info) {
			state.userInfo = info
		}
	},
	actions: {
		getUserInfo({commit}  , that) {
			if(!uni.getStorageSync('token')) return
			that.$u.api.postGetAdminInfo()
				.then(res => {
					if (res.status == true) {
						uni.setStorageSync('userInfo', res.data)
						commit('commitUserInfo', res.data)
						switch(res.data.type) {
							case 0:
							 uni.setStorageSync('role', 'any')
							 break
							case 1 || 2:
							uni.setStorageSync('role', 'small')
							break
							case 3:
							uni.setStorageSync('role', 'big')
						}
					}
				})
		}
	}
})
export default store
